/*
 *  Copyright (C) 2010 Zubanov Dmitry
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 *
 * @author Zubanov Dmitry aka javadimon, zubanov@gmail.com
 * Omsk, Russia, created 21.09.2010
 * (C) Copyright by Zubanov Dmitry
 */
package org.jssdb.core.net;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.zip.GZIPOutputStream;

public class DataChunk implements Serializable, Comparable {

    private long id, part, partCount;
    private Class clazz;
    private byte[] data;

    public DataChunk(long id, long part, long partCount, Class clazz, byte[] data) {
        this.id = id;
        this.part = part;
        this.partCount = partCount;
        this.clazz = clazz;
        this.data = data;
    }

    public Class getDataClass() {
        return clazz;
    }

    public byte[] getData() {
        return data;
    }

    public long getId() {
        return id;
    }

    public long getPart() {
        return part;
    }

    public long getPartCount() {
        return partCount;
    }

    private int getDataLenght() {
        int len = -1;
        if (data != null) {
            len = data.length;
        }

        return len;
    }

    public byte[] getMeGzipBytes() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GZIPOutputStream gz = new GZIPOutputStream(baos);
        ObjectOutputStream oout = new ObjectOutputStream(gz);
        oout.writeObject(this);
        oout.flush();
        oout.close();
        gz.flush();
        gz.close();

        return baos.toByteArray();
    }

    @Override
    public String toString() {
        return MessageFormat.format("Data chunk id={0}, part = {1}, "
                + "part count = {2}, class = {3}, byte data length = {4}",
                new Object[]{id, part, partCount, clazz, getDataLenght()});
    }

    public int compareTo(Object obj) {
        int val = 0;

        if (obj == null) {
            return 0;
        }

        if (getClass() != obj.getClass()) {
            return 0;
        }

        final DataChunk other = (DataChunk) obj;
        if (other.getPart() > part) {
            val = -1;
        } else {
            val = 1;
        }

        return val;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final DataChunk other = (DataChunk) obj;
        if (this.id != other.id) {
            return false;
        }
        if (this.part != other.part) {
            return false;
        }
        if (this.partCount != other.partCount) {
            return false;
        }
        if (this.clazz != other.clazz && (this.clazz == null || !this.clazz.equals(other.clazz))) {
            return false;
        }
        if (!Arrays.equals(this.data, other.data)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        return hash;
    }
}
